package com.ayn.aynsystem.Test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ayn.aynsystem.utils.JsonUtils;
import org.junit.Test;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;

/**
 * @author Ayn
 * @date 2022/5/24 11:10
 */
public class FileTest {


    @Test
    public void testPrintFile() throws IOException {
        File file = new File("C:\\Users\\Admin\\Desktop\\1.txt");

        FileReader fr = new FileReader(file);
        char[] chars = new char[50];
        fr.read(chars);
        for (char c : chars) {
            System.out.println(c);
        }
        // 关闭资源
        fr.close();
    }

    @Test
    public  void testFileToJson(){
        JSONArray provinces= JSON.parseArray(JsonUtils.getJson());
        int cityGeshu = 0;
        int countyGeshu=0;
        for (int i = 0; i < provinces.size(); i++) {
            JSONObject province=provinces.getJSONObject(i);
            JSONArray citys = province.getJSONArray("citys");
            cityGeshu+=citys.size();
            for (int j = 0; j < citys.size(); j++) {
                JSONObject city=citys.getJSONObject(j);
                JSONArray countys = city.getJSONArray("county");
                countyGeshu+=countys.size();
                System.out.println(province.getString("name") +"省,"+city.getString("name")+ "市,"+countys.size()+"个区");
            }
        }

        System.out.println("总共 省个数:"+provinces.size());
        System.out.println("总共 市个数:"+cityGeshu);
        System.out.println("总共 区个数:"+countyGeshu);
    }
}



